////////////////////////////////////////////////////////////////////////////////
// Filename: circle.cpp
////////////////////////////////////////////////////////////////////////////////
#include "circle.hpp"

miv::Circle::Circle() : miv::Polygon() {
	m_position = NULL;
}

miv::Circle::~Circle() {

}

bool miv::Circle::Initialize(float radius, int detail, Vector2D* position, D3DXVECTOR4 color, ID3D11Device* device) {
	Vector2D** vertices = new Vector2D*[detail + 1];
	unsigned long* indices = new unsigned long[detail * 3];
	float div = 360.0f / detail;
	m_radius = radius;
	m_position = position;

	vertices[detail] = new Vector2D(m_position->GetX(), m_position->GetY());
	////////////////////////////////////////
	// THIS IS WHERE THE MAGIC HAPPENS!!! //
    ////////////////////////////////////////
	int j = 0;
	for (int i = 0; i < detail; ++i) {
		float theta = i * (div* M_PI / 180);
		float x = (float)(m_position->GetX() - radius * (float)cos(theta));
		float y = (float)(m_position->GetY() + radius * (float)sin(theta));

		vertices[i] = new Vector2D(x, y);

		indices[j] = i;
		indices[j + 1] = (i + 1) % detail;
		indices[j + 2] = 0;

		j += 3;
	}

	miv::Polygon::Initialize(vertices, indices, detail, detail * 3, color, device);

	delete[] vertices;
	delete[] indices;

	return true;
}

void miv::Circle::Shutdown() {
	m_position = NULL;
	miv::Polygon::Shutdown();
}
